<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业">
              <a-select allowClear v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.zyList" :key="index" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="学生类别"
              >
                <a-select allowClear v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="年级"
              >
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="学制"
              >
                <a-select allowClear v-model="queryParam.xz">
                  <a-select-option v-for="i in TYPE0301" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学号"
              >
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="姓名"
              >
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select allowClear v-model="queryParam.xjztm" placeholder="请选择">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生来源">
                <a-select allowClear v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="录取类别">
                <a-select allowClear v-model="queryParam.lqlbm">
                  <a-select-option v-for="i in TYPE0362" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="毕业年月"
              >
                <a-select allowClear v-model="queryParam.byny" placeholder="请选择">
                  <a-select-option v-for="i in selectMenu.years" :key="i.val" :value="i.val">{{ i.name }}
                  </a-select-option>
                </a-select>
                <!-- <a-month-picker v-model="queryParam.byny" format="YYYY-MM"/> -->
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
             <a-button type="primary" @click="exportExcel" style="margin:0 10px">导出</a-button>
             <a-button type="primary" @click="exportPrint" style="margin:0 10px">批量打印</a-button>
               <a-button type="primary" @click="exportCnPrint" style="margin:0 10px">批量打印中文成绩</a-button>
             <a-button type="primary" @click="exportEnPrint" style="margin:0 10px">批量打印英文成绩</a-button>
             <a-button type="primary" v-if="routeName === '核对学生成绩'" @click="bacthCredit"
                       style="margin:0 10px">批量核对</a-button>
              <!-- <a-button v-if="routeName === '成绩查询打印'" type="primary" style="margin:0 10px" @click="exportExcel">导出Excel</a-button> -->
            <a-button @click="queryParam = {}" style="margin:0 10px">重置</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="options.alert" :rowSelection="options.rowSelection" rowKey="xh"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="action" slot-scope="id, record">
          <a @click="showScore(record.xh)" v-if="routeName === '成绩查询打印'">成绩</a>
          <!-- <a-divider type="vertical" /> -->
          <a @click="showCredit(record.xh)" v-if="routeName === '核对学生成绩'">核对</a>
        </template>
      </s-table>
    </a-card>

    <creditModal
      ref="check"
      :visible="creditVisible"
      :isCredit="isCredit"
      @cancel="creditVisible = false"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import creditModal from './creditModal'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        sszym: '',
        xjztm: ''
      },
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        if (this.routeName === '成绩查询打印') {
          params.kcyx = params.ssyxm
        }
        let apiFn = this.routeName === '成绩查询打印' ? 'scoreSearchList' : 'scoreCreStuList'
        return this.$api.cultivate[apiFn](params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList: [],
        years: []
      },
      creditVisible: false,
      isCredit: false,
      initDone: false
    }
  },
  components: {STable, creditModal},
  computed: {
    ...mapState({
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0301: state => state.app['TYPE0301'],
      TYPE0073: state => state.app['TYPE0073'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0362: state => state.app['TYPE0362'],

    }),
    routeName () {
      return this.$route.name
    },
    columns () {
      if (this.routeName === '成绩查询打印') {
        return [
          {
            title: '序号',
            scopedSlots: { customRender: 'serial' }
          },
          {
            title: '学号',
            dataIndex: 'xh'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '院系',
            dataIndex: 'ssyxmc'
          },
          {
            title: '专业',
            dataIndex: 'sszymc'
          },
          {
            title: '操作',
            scopedSlots: { customRender: 'action' }
          }
        ]
      } else {
        return [
          {
            title: '序号',
            scopedSlots: { customRender: 'serial' }
          },
          {
            title: '学号',
            dataIndex: 'xh'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '院系',
            dataIndex: 'ssyxmc'
          },
          {
            title: '专业',
            dataIndex: 'sszymc'
          },
          {
            title: '操作',
            scopedSlots: { customRender: 'action' }
          }
        ]
      }
    }
  },
  watch: {
    routeName (val) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    }
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0301')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.$store.dispatch('app/setDictionary', 'TYPE0362')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        if (this.selectMenu.yxList && this.selectMenu.yxList.length == 1) {
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yxList[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        let date = new Date();
        let year = date.getFullYear();
        let years = [];
        while (year != 2020) {
          years.push({name: year + "上半年", val: year + ",01"})
          years.push({name: year + "下半年", val: year + ",02"})
          year = year - 1;
        }
        this.$set(this.selectMenu, 'years', years)
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.queryParam.sszym = ''
      this.$set(this.selectMenu, 'zyList', [])
      if (code) {
        this.kcLoading = true
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    showCredit (xh) {
      this.creditVisible = true
      this.isCredit = true
      this.$api.cultivate.scoreCreInfo({xh}).then(res => {
        this.$refs.check.detail.studentInfo = res.data
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    showScore (xh) {
      this.creditVisible = true
      this.isCredit = false
      this.$api.cultivate.scoreStuInfo(xh).then(res => {
        this.$refs.check.detail = res.data
      })
    },
    exportExcel() {
      // this.$download('/foster/kccj/xscjPrintBatch', {
      //   ...this.queryParams
      // })
      this.$export('foster/kccj/cjcxListExport',this.queryParam)
    },
    exportPrint () {
      if (!this.selectedRowKeys.length && (!this.queryParam.ssyxm)) {
        this.$message.warn("当前打印范围过大，请选择`院系`减少打印数据！")
        return;
      }
      let params = {
        ...this.queryParam,
        xhs: this.selectedRowKeys
      }
      this.$export('foster/kccj/xscjPrintBatch', params)
    },
    exportCnPrint() {
      if (!this.selectedRowKeys.length && (!this.queryParam.ssyxm)) {
        this.$message.warn("当前打印范围过大，请选择`院系`减少打印数据！")
        return;
      }
      let params = {
        ...this.queryParam,
        xhs: this.selectedRowKeys
      }
      this.$export('foster/kccj/xscjPrintBatchCn', params)
    },
    exportEnPrint() {
      if (!this.selectedRowKeys.length && (!this.queryParam.ssyxm)) {
        this.$message.warn("当前打印范围过大，请选择`院系`减少打印数据！")
        return;
      }
      let params = {
        ...this.queryParam,
        xhs: this.selectedRowKeys
      }
      this.$export('foster/kccj/xscjPrintBatchEn', params)
    },
    bacthCredit() {
      this.$message.loading('核对中', 0)
      this.$api.cultivate.scoreBatchCredit(this.queryParam).then(res => {
        this.$message.destroy()
        this.$message.success(res.msg)
      })
    }
  }
}
</script>

<style>

</style>
